#include <iostream>
#include <stdio.h>
#include <fstream>
#include <memory>
#include <pplx/pplxtasks.h>
#include <cpprest/asyncrt_utils.h>
#include <cpprest/http_client.h>  // 包含CppRESTSDK的HTTP Client头文件
#include <cpprest/http_listener.h>  // 包含CppRESTSDK的HTTP Listener头文件
#include <cpprest/json.h>  // 包含CppRESTSDK的JSON头文件
#include <cpprest/uri.h>  // 包含CppRESTSDK的URI头文件
#include <cpprest/details/basic_types.h>  // 包含CppRESTSDK的基本类型头文件
#include <cpprest/containerstream.h>  // 包含CppRESTSDK的容器流头文件
#include <cpprest/filestream.h>  // 包含CppRESTSDK的文件流头文件
#include <cpprest/producerconsumerstream.h>  // 包含CppRESTSDK的生产者消费者流头文件
#include <cpprest/ws_client.h>  // 包含CppRESTSDK的WebSocket Client头文件
#include <cpprest/interopstream.h>  // 包含CppRESTSDK的Interop流头文件
#include <cpprest/rawptrstream.h>  // 包含CppRESTSDK的Raw指针流头文件
#include <cpprest/details/web_utilities.h>  // 包含CppRESTSDK的Web Utilities头文件
#include <opencv2/opencv.hpp>
#include <thread>
#include <vector>
#include "DetectionInfer.h"

using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;


#define SERVER_ADDR  "localhost"
#define SERVER_PORT  "8080"
#define RESTFUL_INFER_API "/api/modelinfer"


#define SERVER_URL "http://" SERVER_ADDR ":" SERVER_PORT RESTFUL_INFER_API
/*
std::string base64_encode(const std::string& input) {
    std::string encoded;
    boost::beast::detail::base64_encode(input, encoded);
    return encoded;
}

std::string base64_decode(const std::string& input) {
    std::string decoded;
    boost::beast::detail::base64_decode(input, decoded);
    return decoded;
}
*/

class RestfulClient {
public:
    RestfulClient(const utility::string_t& url);
    void sendAsyncPostRequest(const utility::string_t& clientId, \
		    const utility::string_t& base64Image, \
		    const double objThre, \
		    const http_request &request);

private:
    web::http::client::http_client m_client;
};

class HttpServer {
public:
    HttpServer(const utility::string_t& url);
    
    void start();
    void stop(); 

private:
    virtual void handle_post(http_request request);
    virtual void handle_get(http_request request);

private:
    http_listener m_listener;
    std::shared_ptr<DetectionInfer> m_pdetect;
};

